package com.diaohw.platform.framework.authorization;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.diaohw.platform.common.constant.RequestConstant;
import com.diaohw.platform.common.obj.login.LoginUser;
import com.diaohw.platform.common.obj.login.LoginVo;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static cn.dev33.satoken.stp.StpUtil.*;
import static com.diaohw.platform.framework.authorization.AuthorizationHeaderConst.DEPARTMENT_ID;
import static com.diaohw.platform.framework.authorization.AuthorizationHeaderConst.LOGIN_ID;

public class AuthorizationUtil {
    /**
     * 用户登录接口
     *
     * @param user 登录用户信息
     */
    public static LoginVo login(LoginUser user) {
        StpUtil.login(user.getLoginId(), SaLoginConfig
                .setExtra(RequestConstant.Username, user.getUsername())
                .setExtra(RequestConstant.Nickname, user.getNickname()));
        final String token = getTokenValueByLoginId(user.getLoginId());
        refreshDataMap(user);
        return new LoginVo(token, getTokenName(), user.getLoginId());
    }

    /**
     * 获取登录用户ID
     */
    public static String getLoginId() {
        return StpUtil.getLoginIdAsString();
    }

    /**
     * 获取当前登录用户的身份信息
     *
     * @return 用户信息
     */
    public static LoginUser getLoginUser() {
        Map<String, Object> dataMap = getSession().getDataMap();
        return buildLoginUser(dataMap);
    }


    /**
     * 根据user-ID登录用户的身份信息
     *
     * @return 用户信息
     */
    public static LoginUser getUserInfo(Object userId) {
        Map<String, Object> dataMap = getSessionByLoginId(userId).getDataMap();
        return buildLoginUser(dataMap);
    }

    /**
     * 类型转换
     */
    private static LoginUser buildLoginUser(Map<String, Object> map) {
        JSONObject obj = new JSONObject(map);
        return LoginUser.builder()
                .loginId(obj.getStr(RequestConstant.LoginId))
                .username(obj.getStr(RequestConstant.Username))
                .nickname(obj.getStr(RequestConstant.Nickname))
                .departmentId(obj.getStr(DEPARTMENT_ID))
                .build();

    }

    /**
     * 根据token获取登录用户ID
     *
     * @param authorization token
     * @return 用户Id
     */
    public static String getLoginId(String authorization) {
        return Convert.toStr(getLoginIdByToken(authorization));
    }

    /**
     * 用户名称
     */
    public static String getUserName(Object userId) {
        return getSessionByLoginId(userId).getString(RequestConstant.Username);
    }

    /**
     * 用户所属组织机构
     */
    public static String getDepartmentId(Object userId) {
        return getUserInfo(userId).getDepartmentId();
    }

    /**
     * 刷新用户信息
     *
     * @param user 登录用户信息
     */
    public static void refreshDataMap(LoginUser user) {
        SaSession session = getSessionByLoginId(user.getLoginId());
        refreshDataMap(user, session);
    }

    /**
     * 刷新用户信息
     *
     * @param user 登录用户信息
     */
    public static void refreshDataMap(LoginUser user, SaSession session) {
        Map<String, Object> map = new ConcurrentHashMap<>();
        map.put(LOGIN_ID, user.getLoginId());
        map.put(RequestConstant.Username, user.getUsername());
        map.put(RequestConstant.Nickname, user.getNickname());
        if (StrUtil.isNotBlank(user.getDepartmentId())) {
            map.put(DEPARTMENT_ID, user.getDepartmentId());
        }
        session.refreshDataMap(map);
    }

}
